/*
 * SDN_Q_S_ViewController
 * @description 本文件提供创建相册问题的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供创建相册问题的ViewController
 */

#import "SDN_Q_S_ViewController.h"
#import "AppCommunicationManager.h"

@interface SDN_Q_S_ViewController ()
{
    //done按钮，过程中会发生变化，需要设置全局
    UIButton *_doneBu;
}
@end

@implementation SDN_Q_S_ViewController
@synthesize questionNoticeLabel;

- (void)dealloc
{
    self.questionNoticeLabel = nil;
    _lastanswerStr = nil;
    _lastquestionStr = nil;
    self.questionAndanswerCallBack = nil;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title =NSLocalizedString(@"album_setQuestion", 设置问题);

    self.view.backgroundColor = UIColorWithRGB(237, 239, 241, 1.0);
    
    if (_lastquestionStr.length > 0)
    {
        self.QuestionText.text = _lastquestionStr;
    }
    if (_lastanswerStr.length > 0)
    {
        self.AnswerText.text = _lastanswerStr;
    }
    self.tableview.tableFooterView = nil;
    self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.backgroundView = nil;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.listArr = [[NSArray alloc]init];
    [self.QuestionText addTarget:self action:@selector(textFieldChangeEdit:) forControlEvents:UIControlEventEditingChanged];
    self.QuestionText.placeholder = NSLocalizedString(@"album_questionPlacehoder", nil);
    self.AnswerText.placeholder = NSLocalizedString(@"album_answerPlacehoder", nil);
    self.Question.text = [NSString stringWithFormat:@"%@:",NSLocalizedString(@"kool_question", 问题)];
    [self.Question sizeToFit];
    [self.QuestionText setFrame:CGRectMake(self.Question.frame.size.width + 30, 32, 270 - self.Question.frame.size.width, 21)];
    self.Answer.text = [NSString stringWithFormat:@"%@:",NSLocalizedString(@"kool_answer", 答案)];
    [self.Answer sizeToFit];
    [self.AnswerText setFrame:CGRectMake(self.Answer.frame.size.width + 30, 88, 270 - self.Answer.frame.size.width, 21)];
    self.AnswerText.userInteractionEnabled = YES;
    [self.AnswerText addTarget:self action:@selector(textFieldChangeEdit:) forControlEvents:UIControlEventEditingChanged];
    [self SetBarButtonItem];
    self.questionNoticeLabel.text = NSLocalizedString(@"album_questionNotice", nil);
    [AppComManager getBanBuData:BanBu_Get_Questionlist_Byalbum par:nil delegate:self];
}



- (void)updateLastQuestion:(NSString *)question andAnswer:(NSString *)answer
{
    _lastanswerStr = [answer copy];
    _lastquestionStr = [question copy];
}


#pragma mark 初始化Barbutton
-(void)SetBarButtonItem
{
    _doneBu = [UIButton buttonWithType:UIButtonTypeCustom];
    [_doneBu setTitle:NSLocalizedString(@"confirmNotice", 确定) forState:UIControlStateNormal];
    
    [_doneBu setFrame:CGRectMake(0, 0, 60, 35)];
    _doneBu.userInteractionEnabled = NO;
    [_doneBu setTitleColor:UIColorWithRGB(255, 255, 255, 0.5) forState:UIControlStateNormal];
    _doneBu.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [_doneBu addTarget:self action:@selector(clickOK) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * doneBuItem = [[UIBarButtonItem alloc]initWithCustomView:_doneBu];
    self.navigationItem.rightBarButtonItem = doneBuItem;
}

#pragma mark 点击事件
-(void)clickOK
{
    ////修改：yinlinlin
    [self.view endEditing:YES];
    if ((self.QuestionText.text.length > 0) && (self.AnswerText.text.length > 0))
    {
        if (self.questionAndanswerCallBack)
        {
            self.questionAndanswerCallBack(self.QuestionText.text,self.AnswerText.text);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    //修改完成：yinlinlin
}



#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell== nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.contentView.backgroundColor = UIColorWithRGB(237, 239, 241, 1.0);
        UIImageView * _lineImgV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 45.5, 300, 0.5)];
        _lineImgV.image = [UIImage imageNamed:@"addline.png"];
        [cell.contentView addSubview:_lineImgV];
        _lineImgV.tag = 110;
        
    }
    if (self.listArr.count > 0)
    {
        cell.textLabel.text = [self.listArr objectAtIndex:indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.QuestionText.text = [self.listArr objectAtIndex:indexPath.row];
    //选择问题之后判断问题和答案框，判断确定按钮是否可以点击
    [self textFieldChangeEdit:self.QuestionText];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 46;
}
#pragma mark - 接口返回数据
- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    if(error)
    {
        
    }
    else
    {
        if([AppComManager respondsDic:resDic isFunctionData:BanBu_Get_Questionlist_Byalbum])
        {
            if([[resDic valueForKey:@"ok"] boolValue])
            {
                if ([[resDic valueForKey:@"list"] isKindOfClass:[NSArray class]])
                {
                    self.listArr = [resDic valueForKey:@"list"];
                    [self.tableview reloadData];
                }
                
            }
        }
    }
}

#pragma mark - UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"begin");
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)viewDidUnload {
    [self setTableview:nil];
    [super viewDidUnload];
}
//检测textfield的text变化，当拼音转化成汉字的时候可以检测到
- (void)textFieldChangeEdit:(UITextField *)sender
{
    //问题和答案必须同时存在的时候确定按钮方可点击
    if ((self.QuestionText.text.length > 0) && (self.AnswerText.text.length > 0))
    {
        _doneBu.userInteractionEnabled = YES;
        [_doneBu setTitleColor:UIColorWithRGB(255, 255, 255, 1) forState:UIControlStateNormal];
        
    }
    else
    {
        _doneBu.userInteractionEnabled = NO;
        [_doneBu setTitleColor:UIColorWithRGB(255, 255, 255, 0.5) forState:UIControlStateNormal];

    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
